package cn.com.bluemoon.daps.standard.controller;


import cn.com.bluemoon.daps.api.sys.RemoteSystemDatasourceService;
import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.standard.entity.DapStandardBasic;
import cn.com.bluemoon.daps.standard.entity.DapStandardDataType;
import cn.com.bluemoon.daps.standard.service.DapStandardBasicService;
import cn.com.bluemoon.daps.standard.service.DapStandardDataTypeService;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 数据库字段基础类型数据表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-08-07
 */
@RestController
@Slf4j
@RequestMapping("/dapStandardDataType")
@Api(tags = {"数据类型管理"})
public class DapStandardDataTypeController {

    @Autowired
    DapStandardDataTypeService dapStandardDataTypeService;

    @Resource
    RemoteSystemDatasourceService dapSystemDatasourceService;

    @Autowired
    DapStandardBasicService dapStandardBasicService;

    @GetMapping("/field/list")
    @ApiOperation(value = "新增字段的时候调用该接口，查询字段能使用的数据类型")
    @BmPermission(value = {"getDataTypeList"}, name = "查询字段能使用的数据类型", ignore = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<List<String>> getDataTypeList(@RequestParam @ApiParam("数据源id") @NotNull(message = "数据源id不能为空") String datasourceId,
                                                    @RequestParam(required = false) @ApiParam("标准id") String standardId) {

        // 根据数据源id得到数据库类型
        DapSystemDatasource dapSystemDatasource = dapSystemDatasourceService.getById(datasourceId);
        /*List<String> list = null;*/
        Integer codeType = null;
        if (standardId != null) {
            // 查询标准对应的代码类型
            DapStandardBasic dapStandardBasic = dapStandardBasicService.getBaseMapper().selectOne(new QueryWrapper<DapStandardBasic>().eq("standard_unique_id", standardId)
                    .eq("bm_status", BmStatus.ON.getCode()));
            codeType = dapStandardBasic.getDataType().getCode();
        }
        if(null != dapSystemDatasource && null != dapSystemDatasource.getDatabaseType()){
            List<String> list = dapStandardDataTypeService.getList(dapSystemDatasource.getDatabaseType().getCode(), codeType);
            return ResultBean.ok(list);
        }
        return ResultBean.error("获取数据失败");
    }

   /* @PostMapping("dataType/excel")
    @ApiOperation("上传数据库数据类型接口")
    @BmPermission(value = {"getDataTypeList"}, name = "查询字段能使用的数据类型", ignore = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public void importTableField(@RequestBody MultipartFile file){

        List<DapStandardDataType> list = ImportDataType.readExcel(file);
        System.out.println(list.toString());
        dapStandardDataTypeService.saveOrUpdateBatch(list);
    }*/
}
